﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Article_management_system
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        bool exit_Login = true;//退出提示框处理使用
        private void button1_Click(object sender, EventArgs e)//登录按钮
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))//都为空时
            {
                label5.Text = "用户名不能为空";
                label6.Text = "密码不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text))//用户名为空时
            {
                label5.Text = "用户名不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text))//密码为空时
            {
                label6.Text = "密码不能为空";
                textBox2.Focus();
            }
            else//用户名密码都输入时
            {
                string sql = "SELECT Username,Password FROM Users";
                SqlDataReader dr = Sqldt.Judge(sql);//链接数据库
                bool login = false;//判断用户名密码是否与数据库一致
                while (dr.Read())//循环数据库数据
                {
                    if (textBox1.Text.Equals(dr[0]) && textBox2.Text.Equals(dr[1]))//判断账号密码是否有误
                    {
                        login = true;
                        break;
                    }
                }
                if (login)
                {
                    exit_Login = false;
                    Articles articles = new Articles();
                    articles.Show();
                    //MessageBox.Show("登录成功！", "登录");//跳转最后做
                    this.Visible = false;

                }
                else
                {
                    MessageBox.Show("用户名或密码不正确！\n请重新输入", "登录");
                    textBox1.SelectAll();//全选text1内容
                    textBox1.Focus();//text1获取焦点
                    textBox2.Text = null;//text2清空
                }
            }
            timer1.Enabled = true;//让提示消失使用
        }

        private void timer1_Tick(object sender, EventArgs e)//时间器--让提示信息消失
        {
            label5.Text = null;
            label6.Text = null;
            timer1.Enabled = false;
        }

        private void label8_MouseMove(object sender, MouseEventArgs e)//鼠标移过标签改变颜色
        {
            label8.ForeColor = System.Drawing.Color.Red;
        }

        private void label8_MouseLeave(object sender, EventArgs e)//鼠标离开标签改变颜色
        {
            label8.ForeColor = System.Drawing.Color.Silver;
        }

        private void button2_Click(object sender, EventArgs e)//退出按钮
        {
            this.Close();
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)//关闭窗体弹出提示
        {
            if (exit_Login)
            {
                var hint = MessageBox.Show($"是否退出登录！", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (!hint.Equals(DialogResult.OK))
                {
                    e.Cancel = true;
                }
            }
        }

        private void label8_Click(object sender, EventArgs e)//注册标签
        {
            Signin signin = new Signin();
            signin.ShowDialog();
        }
    }
}
